<?php


namespace app\api\controller\v1;
use app\api\model\v1\MechanismArticlecatalogue;
use app\api\model\v1\MechanismBanner;
use app\api\model\v1\MechanismCatalogue;
use app\api\model\v1\MechanismCategory;
use app\api\model\v1\MechanismCourse;
use app\api\model\v1\MechanismMember;
use app\api\model\v1\MechanismWork;
use app\api\model\v1\UserCheck;
use app\common\controller\Api;
use think\Config;


class Mechanism extends Api
{
    protected $noNeedLogin = ['mechanismCategory','mechanismList','mechanismDetail','mechanismCourseList','mechanismAllList','mechanismCatalogueList'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

        $this->mechanismplan=model('admin/examination/Mechanismplan');




        //mechanism_category

    }
    /**
     * 机构分类
     */
    public function mechanismCategory()
    {
        $list = MechanismCategory::order('weigh asc')->field('id,name')->select();
        $this->success('成功', $list);
    }

    /**
     * 机构列表
     */
    public function mechanismList()
    {
        $limit = $this->request->post('limit') ?? 10;
        $category_id = $this->request->post('category_id'); //行业ID
        $province_name = $this->request->post('province_name'); //省
        $city_name = $this->request->post('city_name'); //市
        $district_name = $this->request->post('district_name'); //区
        $type = $this->request->post('type'); //入驻类型:check=主考机构,evaluation=评价中心,base=实训基地
        $map = $map2 = [];
        if ($category_id) {
            $map2 = "FIND_IN_SET($category_id,courses_category_ids)";
        }
        if ($province_name) {
            $map['province_name'] = ['=', $province_name];
        }
        if ($city_name) {
            $map['city_name'] = ['=', $city_name];
        }
        if ($district_name) {
            $map['district_name'] = ['=', $district_name];
        }
        $map['status'] = ['=', 1];
        $map['type'] = ['=', $type];
        $list = UserCheck::with(['user'=>function($query) {
            $query->field('id,avatar');
        }])->where($map)->where($map2)->order('id desc')->paginate($limit)->toArray();
        $this->success('成功', $list);
    }

    /**
     * 机构列表-不分页
     */
    public function mechanismAllList()
    {
        $category_id = $this->request->post('category_id'); //行业ID
        $type = $this->request->post('type'); //入驻类型:check=主考机构,evaluation=评价中心,base=实训基地
        $map = $map2 = [];
        if ($category_id) {
            $map2 = "FIND_IN_SET($category_id,courses_category_ids)";
        }
        $map['status'] = ['=', 1];
        if ($type) {
            $map['type'] = ['=', $type];
        }
        $list = UserCheck::where($map)->where($map2)->order('id desc')->select();
        $this->success('成功', $list);
    }
    
    /**
     * 机构详情
     */
    public function mechanismDetail()
    {
        $id = $this->request->post('id');
        $checkModel = UserCheck::where('id', $id)->where('status', 1)->find();
        if (!$checkModel) $this->error('机构不存在');
        $work = MechanismWork::where('user_check_id', $id)->order('weigh asc')->field('image')->select();
        $member = MechanismMember::where('user_check_id', $id)->order('weigh asc')->select();
        $this->success('成功', ['mechanism'=>$checkModel,'work'=>$work,'member'=>$member]);
    }

    /**
     * 机构课程列表
     */
    public function mechanismCourseList()
    {
        $limit = $this->request->post('limit') ?? 10;
        $id = $this->request->post('id'); //机构ID
        $category_id = $this->request->post('category_id'); //分类ID
        $sold_sort = $this->request->post('sold_sort'); //销量排序desc(倒序)asc(正序)
        $id_sort = $this->request->post('id_sort'); //最新排序desc(倒序)asc(正序)
        $mechanism_category_topid = $this->request->post('mechanism_category_topid'); //专业ID
        $mechanism_category_id = $this->request->post('mechanism_category_id'); //级别ID
        $checkModel = UserCheck::where('id', $id)->find();
        if (!$checkModel) $this->error('机构不存在');
        $map = [];
        $order = 'id desc';
        if ($category_id) {
            $map['pid'] = ['=', $category_id];
        }
        if ($sold_sort) {
            $order = "sales_num $sold_sort";
        }
        if ($id_sort) {
            $order = "id $id_sort";
        }
        if ($mechanism_category_topid) {
            $map['mechanism_category_top'] = ['=', $mechanism_category_topid];
        }
        if ($mechanism_category_id) {
            $map['mechanism_category_two'] = ['=', $mechanism_category_id];
        }
        $map['user_check_id'] = ['=', $id];
        $list = MechanismCourse::where($map)->order($order)->paginate($limit)->toArray();

        foreach ($list['data'] as $key=>$value){
//dump($value);
            if($this->mechanismplan->where('id',1)->find()){
                $list['data'][$key]['is_ready']=1;
            }else{
                $list['data'][$key]['is_ready']=0;
            }
          //  $list[$key]['is_ready']=0;
        }

        $this->success('成功', $list);
    }

    /**
     * 机构课程详情
     */
    public function mechanismCourseDetail()
    {
        $check_id = $this->request->post('check_id'); //机构ID
        $courses_id = $this->request->post('courses_id'); //课程ID
        $result = MechanismCourse::where('id', $courses_id)->where('user_check_id', $check_id)->find();
        if (!$result) $this->error('课程不存在');
        $this->success('成功', $result);
    }

    /**
     * 视频课程目录
     */
    public function mechanismCatalogueList()
    {
        $check_id = $this->request->post('check_id'); //机构ID
        $courses_id = $this->request->post('courses_id'); //课程ID
        $result = MechanismCourse::where('id', $courses_id)->where('user_check_id', $check_id)->find();
        if (!$result) $this->error('课程不存在');
        $list = MechanismCatalogue::where('mechanism_courses_id', $courses_id)->order('weigh asc')->select();
        $this->success('成功', $list);
    }

    /**
     * 文章课程目录
     */
    public function mechanismArticlecatalogueList()
    {
        $check_id = $this->request->post('check_id'); //机构ID
        $courses_id = $this->request->post('courses_id'); //课程ID
        $result = MechanismCourse::where('id', $courses_id)->where('user_check_id', $check_id)->find();
        if (!$result) $this->error('课程不存在');
        $list = MechanismArticlecatalogue::where('mechanism_courses_id', $courses_id)->order('weigh asc')->select();
        $this->success('成功', $list);
    }
}